﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P2PFileScheduling
{

    public class DirectedEdge
    {
        private Vertex v;
        private Vertex w;

        public Vertex From { get { return v; } set { v = value; } }
        public Vertex To { get { return w; } set { w = value; } }

        public Digraph graph;
        public bool valid;
        public int ID;
        public int flow { get; set; }
        public int type { get; set; }
        public int capacity
        {
            get
            {
                // If this is the super source or sink, there's always
                // room for one more
                if (v == graph.superSource || w == graph.superSink)
                    return this.flow + 1;
                
                List<DirectedEdge> edges = graph.getEdges(type, v);
                int sum = 0;
                
                // Sum the entire flow from this vertex
                if (edges != null)
                {
                    foreach (DirectedEdge e in edges)
                    {
                        //if (e.w == w && e.flow > 0)
                        //    return 0;
                        //else
                            sum += e.flow;
                    }
                }
                else
                    return 0;

                if (Math.Min(this.v.upload, this.w.download)/* - sum*/ > 0)
                    return 1;
                else
                    return 0;
                
            } 
        
        }

        /**
          * Create a directed edge from v to w with given weight.
          */
        public DirectedEdge(Digraph g, Vertex v, Vertex w, int ID, int type)
        {
            this.v = v;
            this.w = w;
            this.ID=ID;
            this.type = type;
            this.graph = g;
            valid = true;
        }

        /**
          * Return the Vertex where this edge begins.
          */
        public Vertex from()
        {
            return v;
        }

        public DirectedEdge reverse(Digraph graph,int ID)
        {
            return new DirectedEdge(graph, this.w, this.v, ID,this.type);
        }

        /**
          * Return the Vertex where this edge ends.
          */
        public Vertex to()
        {
            return w;
        }

        /**
          * Return a string representation of this edge.
          */
        public String toString()
        {
            return v + "->" + w + " ";
        }


    }
}